# -*- coding: utf-8 -*-
import re
from scrapy import Spider, Request, FormRequest
from ..items import WeiboItem
import logging
import json
import datetime
import time
class SearchSpider(Spider):
    name = "search"
    allowed_domains = ["m.weibo.cn"]
    url = '{url}?uid={uid}&luicode={luicode}&lfid={lfid}&type={type}&value={value}&containerid={containerid}&page={page}'
    search_url = 'https://m.weibo.cn/api/container/getIndex'
    max_page = 40
    uid = '1565668374'
    luicode = '10000011'
    lfid = '100103type=1&q=财上海'
    type = 'uid'
    value = '1565668374'
    containerid = '1076031565668374'
    def start_requests(self):
        """crawl the job info from lagou H5 web pages"""
        logging.debug("start")
        for i in range(2, self.max_page + 1):
            time.sleep(2)
            print(self.url.format(url=self.search_url,uid=self.uid, luicode=self.luicode, lfid=self.lfid, type=self.type, value=self.value, containerid=self.containerid, page=str(i)))
            yield Request(
                self.url.format(url=self.search_url, uid=self.uid, luicode=self.luicode, lfid=self.lfid, type=self.type,
                                value=self.value, containerid=self.containerid, page=str(i)), self.parse_index)

    def parse_index(self, response):
        results = json.loads(response.text)
        item = WeiboItem()
        re = results['data']['cards']
        for key in re:
            stri = repr(key['mblog']['text'])
            print(stri.replace("u200b", ""))
            item['text'] = stri.replace("u200b", "")
            # item['createTime'] = self.prase_data(key['mblog']['created_at'])
            item['created_at'] = key['mblog']['created_at']
            item['id'] = key['mblog']['id']
            item['reposts_count'] = key['mblog']['reposts_count']
            item['comments_count'] = key['mblog']['comments_count']
            item['attitudes_count'] = key['mblog']['attitudes_count']
            time.sleep(2)
            yield item


            # def prase_data(self, data_str):
            #     today = datetime.date.today()  # 获得今天的日期
            #     yesterday = today - datetime.timedelta(days=1)  # 用今天日期减掉时间差，参数为1天，获得昨天的日期
            #     if (data_str[0:2] == '今天'):
            #         return str(datetime.datetime.strptime(str(today) + ' ' + data_str[3:], "%Y-%m-%d %H:%M"))
            #         # print(datetime.datetime.strptime(str(today) + ' ' + data_str[3:], "%Y-%m-%d %H:%M"))
            #     if (data_str[0:2] == '昨天'):
            #         # print(datetime.datetime.strptime(str(yesterday) + ' ' + data_str[3:], "%Y-%m-%d %H:%M"))
            #         return str(datetime.datetime.strptime(str(yesterday) + ' ' + data_str[3:], "%Y-%m-%d %H:%M"))
            #
            #     return data_str
